import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import './style.css'
import store from './store'
import PanelHead from './components/panelHead.vue'  // 确保文件名和导入名一致

// 如果您正在使用CDN引入，请删除下面一行。
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
// 刷新后的动态路由添加逻辑

const localData=localStorage.getItem('pz_v3pz')
if(localData){
  store.commit('dynamicMenu',JSON.parse(localData).menu.routerList)
  store.state.menu.routerList.forEach((item)=>{
    router.addRoute('main',item)
  })
}

// 路由守卫 前置导航守卫，在跳转前
router.beforeEach((to, from) => {
  const token = localStorage.getItem('pz_token')
  // 非登录页面，token不存在
  if (!token && to.path !== '/login') {
    return '/login'
  } else if (token && to.path === '/login') {
    return '/'
  } else {
    return true
  }
})

const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
// element-plus挂载
app.use(ElementPlus)
// router挂载
app.use(router)
// store挂载
app.use(store)
// 全局注入
app.component('PanelHead', PanelHead)  // 全局注册为 <PanelHead>
app.mount('#app')


